package com.gitee.ywj1352.common.handler;

import com.gitee.ywj1352.common.constants.CommonConstants;
import com.gitee.ywj1352.common.exception.BaseException;
import com.gitee.ywj1352.common.vo.ResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by wenjie.yang on 6/28/2018.
 */
@ControllerAdvice("com.gitee.ywj1352")
@ResponseBody
public class ExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
    @org.springframework.web.bind.annotation.ExceptionHandler(BaseException.class)
    public ResponseVO baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        logger.error(ex.getMessage(),ex);
        return new ResponseVO(ex.getStatus(), ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public ResponseVO otherExceptionHandler(HttpServletResponse response, Exception ex) {
        logger.error(ex.getMessage(),ex);
        return new ResponseVO(CommonConstants.EX_OTHER_CODE, ex.getMessage());
    }
}
